package com.focus.xyz.mobile.pages;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.utils.Constants;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import java.util.List;

/**
 * Created by chenmeng on 2016/7/26.
 */
public class MProdCataPage {

    private Action action;

    @FindBy(xpath = "//div[@class='elements']/a")
    private List<WebElement> catalog;
    @FindBy(xpath = "//ul[@class='list-items']/li/a")
    private List<WebElement> prodList;
    @FindBy(xpath = "//div[@class='pager']/div/ul/li[3]/a")
    private WebElement nextPage;

    public MProdCataPage(WebDriver driver,Action action){
        this.action = action;
        PageFactory.initElements(driver,this);
    }

    public void clickCatalog(String cataName) throws Exception{
        for(WebElement e : catalog){
            if(cataName.equals(e.getAttribute("title").trim())){
                action.click(e);
                break;
            }
        }
    }

    public void clickProd(String prodName) throws Exception{
        for(WebElement e:prodList){
            WebElement insName = e.findElement(By.xpath("div[@class='ins-name']"));
            if(prodName.equals(insName.getText().trim())){
                action.click(e);
                return;
            }
        }
        if(!nextPage.getAttribute("href").contains("javascript:void(0);")){
            action.click(nextPage);
            clickProd(prodName);
        }
    }


}
